package com.aphrodite.ui.widgets.chart;

import javax.microedition.lcdui.Graphics;

import com.aphrodite.util.geom.Rectangle;

/**
 * Represents a class that uses a background scale
 * 
 * @author kentelt
 */
public abstract class ScaleChart extends Chart {

	/**
	 * Represents a block scale in the X axis
	 */
	public static final int BG_BLOCK_X = 1;
	/**
	 * Represents a block scale in the Y axis
	 */
	public static final int BG_BLOCK_Y = 2;
	/**
	 * Represents a dotted line scale in the x axis
	 */
	public static final int BG_LINE_DOTTED_X = 8;
	/**
	 * Represents a dotted line scale in the y axis
	 */
	public static final int BG_LINE_DOTTED_Y = 4;
	/**
	 * Represents a line scale in the x axis
	 */
	public static final int BG_LINE_X = 16;
	/**
	 * Represents a dotted line scale in the y axis
	 */
	public static final int BG_LINE_Y = 32;

	protected Rectangle bgArea;
	// |
	// BG_LINE_DOTTED_Y;
	private int scaleColor = 0xC0C0C0;
	private int scaleType = BG_LINE_DOTTED_X | BG_LINE_Y;// BG_LINE_DOTTED_X
	private int xScaleUnit = 10;
	private int yScaleUnit = 10;

	protected ScaleChart(int width, int height) {
		super(null, width, height);
	}

	protected void capture(Graphics g, int width, int height, boolean drawBG) {
		super.capture(g, width, height, drawBG);
		drawScale(g);
	}

	/**
	 * Call after translation of origin
	 * 
	 * @param g
	 */
	private void drawScale(Graphics g) {
		if (bgArea != null) {
			g.setColor(scaleColor);
			int xIter = (width - bgArea.getX()) / xScaleUnit;
			int currX = 0;
			g.translate(bgArea.getX(), bgArea.getY());
			boolean draw = true;
			/* Solid vertical blocks */
			if ((scaleType & BG_BLOCK_X) == BG_BLOCK_X) {
				for (int i = 0; i < xIter; i++) {
					if (draw) {
						g.fillRect(i * xScaleUnit, 0, xScaleUnit, bgArea.getHeight());
						draw = false;
					} else {
						draw = true;
					}
				}
				draw = true;

			}
			if ((scaleType & BG_BLOCK_Y) == BG_BLOCK_Y) {
				int ht = bgArea.getHeight();
				/* start drawing the rectangle from the bottom */
				for (; ht > 0;) {
					if (draw) {
						g.fillRect(0, ht - yScaleUnit, bgArea.getWidth(), yScaleUnit);
						draw = false;
					} else {
						draw = true;
					}
					ht -= yScaleUnit;
				}
				draw = true;
			}
			if ((scaleType & BG_LINE_DOTTED_X) == BG_LINE_DOTTED_X || (scaleType & BG_LINE_X) == BG_LINE_X) {
				System.out.println("drawing horizontal dotted line");
				g.setStrokeStyle((scaleType & BG_LINE_X) == BG_LINE_X ? Graphics.SOLID : Graphics.DOTTED);
				for (int i = 0; i < xIter; i++) {
					if (draw) {
						g.drawLine(i * xScaleUnit, 0, i * xScaleUnit, bgArea.getHeight());
						draw = false;
					} else {
						draw = true;
					}
					currX += xScaleUnit;
				}
				currX = 0;
				draw = true;
				g.setStrokeStyle(Graphics.SOLID);
			}
			if ((scaleType & BG_LINE_DOTTED_Y) == BG_LINE_DOTTED_Y || (scaleType & BG_LINE_Y) == BG_LINE_Y) {
				System.out.println("drawing vertical dotted line");
				g.setStrokeStyle((scaleType & BG_LINE_Y) == BG_LINE_Y ? Graphics.SOLID : Graphics.DOTTED);
				int ht = bgArea.getHeight();
				for (; ht > 0;) {
					if (draw) {
						g.drawLine(0, ht, bgArea.getWidth(), ht);
						draw = false;
					} else {
						draw = true;
					}
					ht -= yScaleUnit;
				}
				draw = true;
				g.setStrokeStyle(Graphics.SOLID);
			}
			g.translate(-bgArea.getX(), -bgArea.getY());
		}
	}

	/**
	 * Set the type of scale to render
	 * 
	 * @param type
	 *            Scale Type. The default is
	 *            {@link BarChart#SCALE_LINE_DOTTED_X} |
	 *            {@link BarChart#BG_LINE_DOTTED_Y}
	 * @throws IllegalArgumentException
	 *             If an invalid scale combination is attempted. For instance
	 *             trying to combine solid and dotted scales with different axis
	 */
	public void setChartBGType(int type) {
		if ((type & BG_BLOCK_X) == BG_BLOCK_X || (type & BG_BLOCK_Y) == BG_BLOCK_Y) {
			if ((type & BG_LINE_DOTTED_X) == BG_LINE_DOTTED_X || (type & BG_LINE_DOTTED_Y) == BG_LINE_DOTTED_Y
					|| (type & BG_LINE_X) == BG_LINE_X || (type & BG_LINE_Y) == BG_LINE_Y) {
				throw new IllegalArgumentException("Cannot combina BG_BLOCK with other BG types");
			}
		}
		this.scaleType = type;
	}

	/**
	 * Set the color of the scale lines
	 * 
	 * @param color
	 */
	public void setScaleColor(int color) {
		this.scaleColor = color;
		reDrawWidget();
	}

	/**
	 * 
	 * @param unit
	 *            The number of pixels for x scale
	 */
	protected void setXscaleUint(int unit) {
		if (unit > 0)
			this.xScaleUnit = unit;
	}

	/**
	 * 
	 * @param unit
	 *            The number of pixels for x scale
	 */
	protected void setYscaleUint(int unit) {
		if (unit > 0)
			this.yScaleUnit = unit;
	}

}
